package drone.iqgc.config ;

import java.lang.reflect.Field ;

import org.springframework.beans.factory.annotation.Value ;
import org.springframework.context.annotation.Configuration ;

import drone.basis.dictionary.CountyDict ;
import drone.basis.util.DateUtil ;
import drone.basis.util.StringUtil ;
import drone.basis.util.WeatherUtil ;
import drone.iqgc.common.ContextAware ;

@Configuration
public class HostConfig
{

	public static String REALM = "zlszgs" ;

	public static String DOMAIN = "drone" ;

	public static String COMPANY = "DRONE" ;

	public static String NAME = "模拟" ;

	public static String IP = "127.0.0.1" ;

	public static String WEATHER = "{}" ;

	public static String PRODUCT_WEBSITE = "https://%s-%s." + REALM + ".com/" ;

	public static String PRODUCT_WEBSOCKET = "wss://%s-%s." + REALM + ".com/" ;

	public static String DEVELOP_WEBSITE = "http://%s-%s." + REALM + "-test.com:%s%s/" ;

	public static String DEVELOP_WEBSOCKET = "ws://%s-%s." + REALM + "-test.com:%s%s/" ;

	// 模拟
	public static String DRONE_MOCK = "https://mock." + REALM + ".com/" ;

	// 构建
	public static String DRONE_BUILD = "https://build." + REALM + ".com/" ;

	// 短信 23000
	public static String DRONE_SMS = "https://sms." + REALM + ".com/" ;

	// 监控 24000
	public static String DRONE_MONITOR = "https://monitor." + REALM + ".com/" ;

	// 资源 25000
	public static String DRONE_STATIC = "https://static." + REALM + ".com/" ;

	// 官网 26000
	public static String DRONE_WWW = "https://www." + REALM + ".com/" ;

	// 入口 27000
	public static String DRONE_PORTAL ;

	// 通讯 28000
	public static String DRONE_RPC ;

	public static String DRONE_SOCKET_RPC ;

	// 地面站 29000
	public static String DRONE_IQGC ;

	@Value ( "${info.company}" )
	public void setCompany (
			String company
	)
	{
		COMPANY = company ;
	}

	@Value ( "${info.name}" )
	public void setName (
			String name
	)
	{
		NAME = name ;
	}

	@Value ( "${info.ip}" )
	public void setIp (
			String ip
	)
	{
		IP = ip ;
	}

	@Value ( "${info.domain}" )
	public void setDomain (
			String domain
	)
	{

		DOMAIN = domain ;

		WEATHER = WeatherUtil.recent ( DOMAIN ) ;

		System.out.println ( DateUtil.format ( ) + " " + WEATHER ) ;

		if ( ContextAware.getActiveProfile ( ).equals ( "production" ) || ContextAware.getActiveProfile ( ).endsWith ( "production" ) )
		{

			String seq = CountyDict.seq ( ContextAware.getActiveProfile ( ).split ( "-" ) [ 0 ] ) ;

			DRONE_RPC = String.format ( "http://%s-rpc:%s%s/" , domain , "280" , seq ) ;

			DRONE_SOCKET_RPC = String.format ( PRODUCT_WEBSOCKET , "rpc" , domain ) ;

			DRONE_PORTAL = String.format ( PRODUCT_WEBSITE , "portal" , domain ) ;

			DRONE_IQGC = String.format ( PRODUCT_WEBSITE , "iqgc" , domain ) ;

		}

		if ( ContextAware.getActiveProfile ( ).equals ( "develop" ) || ContextAware.getActiveProfile ( ).endsWith ( "develop" ) )
		{

			String seq = CountyDict.seq ( ContextAware.getActiveProfile ( ).split ( "-" ) [ 0 ] ) ;

			DRONE_RPC = String.format ( DEVELOP_WEBSITE , "rpc" , domain , "280" , seq ) ;

			DRONE_SOCKET_RPC = String.format ( DEVELOP_WEBSOCKET , "rpc" , domain , "280" , seq ) ;

			DRONE_PORTAL = String.format ( DEVELOP_WEBSITE , "portal" , domain , "270" , seq ) ;

			DRONE_IQGC = String.format ( DEVELOP_WEBSITE , "iqgc" , domain , "290" , seq ) ;

		}

		System.out.println ( "DRONE_RPC        " + DRONE_RPC ) ;
		System.out.println ( "DRONE_SOCKET_RPC " + DRONE_SOCKET_RPC ) ;
		System.out.println ( "DRONE_PORTAL     " + DRONE_PORTAL ) ;
		System.out.println ( "DRONE_IQGC      " + DRONE_IQGC ) ;

	}

	public static String URL (
			String sign
	)
	{

		if ( StringUtil.isEmpty ( sign ) )
		{
			return DRONE_PORTAL ;
		}

		for ( Field field : HostConfig.class.getDeclaredFields ( ) )
		{
			try
			{
				field.setAccessible ( true ) ;

				if ( field.getType ( ) == String.class && field.getName ( ).endsWith ( sign ) )
				{
					return String.valueOf ( field.get ( HostConfig.class ) ) ;
				}
			} catch ( Exception e )
			{
				e.printStackTrace ( ) ;
				continue ;
			}
		}

		return DRONE_PORTAL ;
	}

	public static void main (
			String [ ] args
	)
	{

		for ( CountyDict d : CountyDict.values ( ) )
		{

			String domain = d.name ( ).toLowerCase ( ) ;
			String seq = d.getSeq ( ) ;

			String portal = String.format ( DEVELOP_WEBSITE , "portal" , domain , "270" , seq ).replace ( "http://" , "" ) ;
			String rpc = String.format ( DEVELOP_WEBSITE , "rpc" , domain , "280" , seq ).replace ( "http://" , "" ) ;
			String iqgc = String.format ( DEVELOP_WEBSITE , "iqgc" , domain , "290" , seq ).replace ( "http://" , "" ) ;

			portal = "127.0.0.1 " + portal.substring ( 0 , portal.indexOf ( ":" ) ) ;
			iqgc = "127.0.0.1 " + iqgc.substring ( 0 , iqgc.indexOf ( ":" ) ) ;
			rpc = "127.0.0.1 " + rpc.substring ( 0 , rpc.indexOf ( ":" ) ) ;

			System.out.println ( portal ) ;
			System.out.println ( iqgc ) ;
			System.out.println ( rpc ) ;
			System.out.println ( ) ;
		}

	}

}
